import type { Action, ThunkAction } from "@reduxjs/toolkit"
import { combineSlices, configureStore } from "@reduxjs/toolkit"
import { setupListeners } from "@reduxjs/toolkit/query"
import { LoginStore } from "../redux/loginStore"
import storage from "redux-persist/lib/storage";
import{persistReducer} from "redux-persist"

// redux-persist持久化配置选项
const persistConfig = {
  key: 'root',
  storage,
};


// 将所有redux整合
// 将所有slices合并到一起，提取出rootstate供hooks使用
const rootReducer = combineSlices(LoginStore )
//将redux持久化
const persistedReducer = persistReducer(persistConfig,rootReducer)
export type RootState = ReturnType<typeof rootReducer>

// The store setup is wrapped in `makeStore` to allow reuse
// when setting up tests that need the same store config
export const makeStore = () => {
  const store = configureStore({
    reducer: persistedReducer,
    // Adding the api middleware enables caching, invalidation, polling,
    // and other useful features of `rtk-query`.
    middleware: getDefaultMiddleware =>
      getDefaultMiddleware({
        serializableCheck: false,
    })
  })
  // configure listeners using the provided defaults
  // optional, but required for `refetchOnFocus`/`refetchOnReconnect` behaviors
  setupListeners(store.dispatch)
  return store
}

export const store = makeStore()

// Infer the type of `store`
export type AppStore = typeof store
// Infer the `AppDispatch` type from the store itself
export type AppDispatch = AppStore["dispatch"]
export type AppThunk<ThunkReturnType = void> = ThunkAction<
  ThunkReturnType,
  RootState,
  unknown,
  Action
>